
// 时间格式化
Date.prototype.pattern = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份         
        "d+": this.getDate(), //日         
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时         
        "H+": this.getHours(), //小时         
        "m+": this.getMinutes(), //分         
        "s+": this.getSeconds(), //秒         
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度         
        "S": this.getMilliseconds() //毫秒         
    };
    var week = {
        "0": "/u65e5",
        "1": "/u4e00",
        "2": "/u4e8c",
        "3": "/u4e09",
        "4": "/u56db",
        "5": "/u4e94",
        "6": "/u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

// 获取我的订单
var getState = function(state) {
        var state = state;
        // console.log(state)
        $.ajax({
            url: "testJson/repairOrder_queryRepaidRecord.json",
            data: { "status": state },
            // method:"POST",
            success: function(data) {
                // console.log(111)
                if (!data.success) {
                    alert("数据获取失败，请检查网络")
                    return
                }
                // console.log(data.repairOrders)
                var orderList = data.repairOrders;
                var html = orderList.map(function(item, index) {
                    // 处理图片
                    var imgUrl = item.rapairPicUrls.split(",")[0]
                        // 处理时间
                    var date = new Date(item.createDate);
                    var orderDate = date.pattern("yyyy-MM-dd");
                    // 处理状态，后面改成function
                    switch (item.orderStatus) {
                        case 1:
                            var orderStatus = "待派件";
                            break;
                        case 2:
                            var orderStatus = "预约成功";
                            break;
                        case 3:
                            var orderStatus = "维修中";
                            break;
                        case 4:
                            var orderStatus = "待评价"

                    }
                    // console.log(item.createDate)
                    return '<div href="javascript:void(0);" data-orderId="' + item.id + '" class="order-list">' +
                        '<p class="order-state"><span>' + orderDate + '</span><span>' + orderStatus + '</span></p>' +
                        '<div class="order-wrapper">' +
                        '<div class="weui-media-box__bd order-content">' +
                        '<h4 class="weui-media-box__title">' + item.questionOne + '</h4>' +
                        '<p class="weui-media-box__desc">' + item.describe + '</p>' +
                        '</div>' +
                        '</div>' +
                        '</div>'
                }).join("");
                $("#myOrder").append(html)

                $(".order-list").on("click", function() {
                    $.alert(222)
                    $(".firstPage").hide();
                    var orderId = $(this).data('orderid');
                    orderDetial(orderId);
                })

            }
        })
    }
    // 获取订单详情
function orderDetial(id) {
    $.ajax({
        url: './testJson/orderId.json',
        data: { "repairOrderId": id },
        success: function(item) {
            var data = item.repairOrder;
            var technician=item.technician;
            console.log(technician)
            var imgUrl=data.rapairPicUrls.split(",");
            console.log(imgUrl);
            var imgEle=imgUrl.map(function(item){
                return "<li><img src='"+item+"' alt=''></li>"
            }).join("");
            console.log(imgEle)
  var html2 ="<div class='detail-content'>"+
    "<div class='weui-cells'>"+
      "<div class='weui-cell'>"+
        "订单编号: <span class='order-number'>"+data.orderNumber+"</span><span class='order-state'></span>"+
      '</div>'+
      "<div class='weui-cell'>"+
        "<p>"+
          "<span>"+data.questionOne+"--</span><span>"+data.questionTwo+"--</span><span>"+data.questionThree+"</span>"+
        "</p>"+
      "</div>"+
      "<div class='weui-cell'>"+
        "<ul class='img-list'>"+
          imgEle+
        "</ul>"+
      "</div>"+
      "<div class='weui-cell'>"+
        "<div class='weui-cell__bd'>"+data.describe+"</div>"+
      "</div>"+
    "</div>"+
    "<div class='weui-cells'>"+
      "<div class='weui-cell'>"+
        "<div class='weui-cell__hd'>"+
          "<i class='fa icon-map-marker'></i>"+
        "</div>"+
        "<div class='weui-cell__bd'>"+
          "<p>"+
            "<span class='user-name'>"+data.userName+"</span><span class='user-phone'>12345678909</span>"+
          "</p>"+
          "<p class='user-local'>"+data.city+data.area+data.buildingName+data.buildingStage+data.buildingNum+data.buildingHouseNum+"</p>"+
        "</div>"+
      "</div>"+
    "</div>"+

    "<div class='weui-cells'>"+
      "<div href='javascript:void(0);' class='weui-cell'>"+
        "<div class='weui-cell__hd order-img'>"+
          "<img class='weui-media-box__thumb' src='img/icon.png' alt=''>"+
        "</div>"+
        "<div class='weui-cell__bd order-content'>"+
          "<h4 class=''>"+technician.userName+"<span class='user-phone'>"+technician.cellphone+"</span></h4>"+
          "<p >XX公司</p>"+
        "</div>"+
      "</div>"+
    "</div>"+
  "</div>"+

  "<a href=rate.html#"+data.id+"><button class='weui-btn weui-btn_primary' >评价</button></a>"
$(".secondPage").append(html2)
        }
    })
}
//  "<a src='"+data.id+"' alt=''>"+
//"<a href='"+data.id+"' alt=''>"+

$(function() {
    getState(1);
    $(".weui-flex__item").on("click", function() {
        var dataState = $(this).data('state');
        $(this).find("i").addClass("active");
        $(this).siblings().find("i").removeClass("active");
        // console.log(sub)
        $("#myOrder").html("")
        getState(dataState);
    })
})
